package com.atguigu.tingshu.order.strategy.impl;
import java.util.UUID;

import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.strategy.SaveOrderStrategy;
import com.atguigu.tingshu.order.template.SaveOrderTemplate;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AccountSaveOrderStrategy extends SaveOrderTemplate implements SaveOrderStrategy {
    private final UserAccountFeignClient userAccountFeignClient;
    private final OrderInfoService orderInfoService;
    private final KafkaService kafkaService;
    public AccountSaveOrderStrategy(UserAccountFeignClient userAccountFeignClient, OrderInfoService orderInfoService, KafkaService kafkaService) {
        this.userAccountFeignClient = userAccountFeignClient;
        this.orderInfoService = orderInfoService;
        this.kafkaService = kafkaService;
    }

    @Override
    public OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) {
        log.info("AccountSaveOrderStrategy...saveOrder...方法执行了..");
        // UUID 生成 订单号
        String orderNo = UUID.randomUUID().toString().replace("-", "");
        // 用户id
        Long userId = AuthContextHolder.getUserId();
        try {
            // 创建 锁定金额对象
            AccountLockVo accountLockVo = new AccountLockVo();
            accountLockVo.setOrderNo(orderNo);
            accountLockVo.setUserId(userId);
            // 锁定金额
            accountLockVo.setAmount(orderInfoVo.getOrderAmount());
            accountLockVo.setContent(orderInfoVo.getOrderDetailVoList().get(0).getItemName());

            Result<AccountLockResultVo> accountLockResultVoResult = userAccountFeignClient.checkAndLock(accountLockVo);
            if(accountLockResultVoResult.getCode().intValue() != 200) {
                throw new GuiguException(accountLockResultVoResult.getCode() , accountLockResultVoResult.getMessage()) ;
            }

            // 保存订单数据
            this.saveOrderAndDetailAndOrderDerate(orderInfoVo , orderNo);
        } catch (GuiguException e) {
            //  异常手动解锁账户
            kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_UNLOCK , orderNo);

            //抛出异常
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        // 返回
        return orderInfoVo;
    }
    /**
     * @description: 保存订单数据后的 后续操作 保存交易信息(保存购买信息user_paid_album、user_paid_track、user_vip_service)
     * @author Petrichor
     * @date 2024/1/8 21:24
     * @param orderNo 订单编号
     * @return NULL
    **/
    @Override
    public void saveOrderAfterProcess(String orderNo) {
        // 更改订单的状态为已支付 ，保存交易信息(保存购买信息user_paid_album、user_paid_track、user_vip_service)
        orderInfoService.orderPaySuccess(orderNo) ;
        // 发送扣减余额消息
        kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_MINUS,orderNo);
    }

    @Override
    public String getPayWay() {
        // 余额支付
        return SystemConstant.ORDER_PAY_ACCOUNT;
    }
}